package com.ruoyi.project.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtil;
import com.ruoyi.project.applet.domain.VipLevel;
import com.ruoyi.project.applet.domain.Wallet;
import com.ruoyi.project.applet.mapper.VipLevelMapper;
import com.ruoyi.project.applet.service.impl.WalletServiceImpl;
import com.ruoyi.project.system.domain.SysUserWx;
import com.ruoyi.project.system.mapper.SysUserWxMapper;
import com.ruoyi.project.system.service.ISysUserWxService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 用户信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-06-06
 */
@Service
public class SysUserWxServiceImpl implements ISysUserWxService 
{

    private static final Logger logger = LoggerFactory.getLogger("SysUserWxServiceImpl");

    @Autowired
    private SysUserWxMapper sysUserWxMapper;


    @Autowired
    private WalletServiceImpl walletService;

    @Autowired
    private VipLevelMapper vipLevelMapper;
    /**
     * 查询用户信息
     * 
     * @param userId 用户信息ID
     * @return 用户信息
     */
    @Override
    public SysUserWx selectSysUserWxById(Long userId)
    {
        SysUserWx userWx = sysUserWxMapper.selectSysUserWxById(userId);
        return userWx;
    }

    /**
     * 查询wx用户信息
     *
     * @param openIds 用户信息ID
     * @return 用户信息
     */
    @Override
    public  List<SysUserWx> selectSysUserWxByOpenIds(List<String> openIds) {
        if (CollectionUtils.isEmpty(openIds)) {
            return Collections.EMPTY_LIST;
        }
        return sysUserWxMapper.selectSysUserWxByOpenIds(openIds);
    }

    @Override
    public List<SysUserWx> selectSysUserWxByParentId(String parentId) {
        if (StringUtil.isEmpty(parentId)) {
            return Collections.EMPTY_LIST;
        }
        return sysUserWxMapper.selectSysUserWxByParentId(parentId);

    }

    @Override
    public List<SysUserWx> selectSysUserWxByUserIds(List<Long> userIds) {
        if(CollectionUtils.isEmpty(userIds)){
            return Collections.emptyList();
        }
        return sysUserWxMapper.selectSysUserWxByUserIds(userIds);
    }


    public SysUserWx selectSysUserWxByOpenId(String openId) {
        return sysUserWxMapper.selectSysUserWxByOpenId(openId);
    }
    public SysUserWx selectSysUserWxByNumberId(String numberId) {
        return sysUserWxMapper.selectSysUserWxByNumberId(numberId);
    }


    /**
     * 查询wx用户信息
     *
     * @param phonenumber 用户手机号
     * @return 用户信息
     */
    @Override
    public SysUserWx selectSysUserWxByPhone(String phonenumber) {
        return sysUserWxMapper.selectSysUserWxByPhone(phonenumber);
    }

    /**
     * 查询用户信息列表
     * 
     * @param sysUserWx 用户信息
     * @return 用户信息
     */
    @Override
    public List<SysUserWx> selectSysUserWxList(SysUserWx sysUserWx)
    {
        List<SysUserWx> sysUserWxList = sysUserWxMapper.selectSysUserWxList(sysUserWx);
        return sysUserWxList;
    }

    /**
     * 新增用户信息
     * 
     * @param sysUserWx 用户信息
     * @return 结果
     */
    @Override
    public int insertSysUserWx(SysUserWx sysUserWx)
    {

        sysUserWx.setUpdateTime(DateUtils.getNowDate());
        sysUserWx.setCreateTime(DateUtils.getNowDate());
        return sysUserWxMapper.insertSysUserWx(sysUserWx);
    }

    /**
     * 修改用户信息
     * 
     * @param sysUserWx 用户信息
     * @return 结果
     */
    @Override
    public int updateSysUserWx(SysUserWx sysUserWx)
    {
        sysUserWx.setUpdateTime(DateUtils.getNowDate());
        sysUserWx.setUpdateTime(DateUtils.getNowDate());
        //修改用户身份为会员
        if (Constants.IS_VIP.equals(sysUserWx.getIsVip())) {
            if (sysUserWx.getExpirationTime() == null) {
                Date currentDate = new Date();
                // 使用Calendar类来操作日期
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(currentDate);
                calendar.add(Calendar.DAY_OF_MONTH, 30); // 加上30天

                // 获取新的日期
                Date newDate = calendar.getTime();
                sysUserWx.setExpirationTime(newDate);
            } else {
                Date expirationTime = sysUserWx.getExpirationTime();
                // 使用Calendar类来操作日期
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(expirationTime);
                calendar.add(Calendar.DAY_OF_MONTH, 30); // 加上30天

                // 获取新的日期
                Date newDate = calendar.getTime();
                sysUserWx.setExpirationTime(newDate);
            }

            logger.info("修改用户:" + sysUserWx.getUserId() + "身份为会员");
        }

        //修改身份为非会员
        if (Constants.NOT_VIP.equals(sysUserWx.getIsVip())) {
            sysUserWxMapper.updateVip(sysUserWx.getUserId());
            logger.info("修改用户:" + sysUserWx.getUserId() + "身份为普通用户");
        }
        return sysUserWxMapper.updateSysUserWx(sysUserWx);
    }

    @Override
    public int updateParent(String openId, List<String> openIds) {
        if (openIds.isEmpty()) {
            return 0;
        }

        return sysUserWxMapper.updateParent(openId,openIds);
    }

    /**
     * 批量删除用户信息
     * 
     * @param userIds 需要删除的用户信息ID
     * @return 结果
     */
    @Override
    public int deleteSysUserWxByIds(Long[] userIds)
    {
        return sysUserWxMapper.deleteSysUserWxByIds(userIds);
    }

    /**
     * 删除用户信息信息
     * 
     * @param userId 用户信息ID
     * @return 结果
     */
    @Override
    public int deleteSysUserWxById(Long userId)
    {
        return sysUserWxMapper.deleteSysUserWxById(userId);
    }
    @Override
    public String getMax()
    {
        final String max = sysUserWxMapper.getMax();
        return max == null ? "0" : max;
    }

    @Override
    public SysUserWx selectSysUserWxByToken(Long userId) {
        SysUserWx sysUserWx = sysUserWxMapper.selectSysUserWxById(userId);
        Wallet walletMoney = walletService.getWalletMoney(sysUserWx.getOpenId());
        sysUserWx.setWallet(walletMoney);
        if (Constants.IS_VIP.equals(sysUserWx.getIsVip())) {
            List<VipLevel> vipLevelList = vipLevelMapper.selectVipLevelList(new VipLevel());
            //设置会员下一等级
            for (int i = 0; i < vipLevelList.size(); i++) {
                VipLevel vipLevel = vipLevelList.get(i);
                if (vipLevel.getName().equals(sysUserWx.getVipLevel())) {
                    if (i < vipLevelList.size() - 1) {
                        sysUserWx.setNextVipLevel(vipLevelList.get(i + 1).getName());
                    } else {
                        sysUserWx.setNextVipLevel(vipLevelList.get(vipLevelList.size()-1).getName());
                    }
                }
            }

        }
        return sysUserWx;
    }

    @Override
    public void resetCheckIn() {
        sysUserWxMapper.resetCheckIn();
    }

    @Override
    public void updateParentId(String openId) {
        sysUserWxMapper.updateParentId(openId);
    }


}
